package com.xingkeduo.dao.database.mybatis.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.EnumTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.xingkeduo.util.pojo.base.BaseEnum;

public class AutoEnumTypeHandler<E extends Enum<E> & BaseEnum> extends BaseTypeHandler<E> {
	private BaseTypeHandler<E> typeHandler = null;

	public AutoEnumTypeHandler(Class<E> type) {
		if (type == null) {
			throw new IllegalArgumentException("Type argument cannot be null");
		}System.out.println("222222222222222222222==========================="+BaseEnum.class.isAssignableFrom(type));
		if (BaseEnum.class.isAssignableFrom(type)) {
			// 如果实现了 BaseCodeEnum 则使用我们自定义的转换器
			typeHandler = new IntEnumValueHandler(type);
		} else {
			// 默认转换器 也可换成 EnumOrdinalTypeHandler
			typeHandler = new EnumTypeHandler(type);
		}
	}

	@Override
    public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
        typeHandler.setNonNullParameter(ps,i, parameter,jdbcType);
    }

    @Override
    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return (E) typeHandler.getNullableResult(rs,columnName);
    }

    @Override
    public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return (E) typeHandler.getNullableResult(rs,columnIndex);
    }

    @Override
    public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return (E) typeHandler.getNullableResult(cs,columnIndex);
    }
}
